﻿@using PagedList.Mvc;
@model Model.Sys.Sys_RoleModel
@{
    PagedList.IPagedList<Model.Origins.OriginsTempletModel> myPageList = (PagedList.IPagedList<Model.Origins.OriginsTempletModel>)ViewBag.MyPageList;
    Layout = "~/Views/Shared/MyLayOutNew.cshtml";
}
 
<link href="~/Content/zTree/css/zTreeStyle/metro.css" rel="stylesheet" />

<script src="~/Content/zTree/js/jquery-1.4.4.min.js"></script>
<script src="~/Content/zTree/js/jquery.ztree.all-3.5.min.js"></script>
<script>
    var zTree;
    var demoIframe;

    function addHoverDom(treeId, treeNode) {
        var sObj = $("#" + treeNode.tId + "_span");
        if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0) return;
        //var addStr = "<span class='button remove' id='removeBtn_" + treeNode.tId
        //        + "' title='add node' onfocus='this.blur();'></span>";

        //addStr += "<span class='button add' id='addBtn_" + treeNode.tId + "'></span>";
        //addStr += "<span class='button edit' id='editBtn_" + treeNode.tId + "'></span>";
        //  sObj.after(addStr);
        sObj.after("");
        var btn = $("#addBtn_" + treeNode.tId);
        if (btn) btn.bind("click", function () {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            zTree.addNodes(treeNode, { id: (1000 + newCount), pId: treeNode.id, name: "new node" + (newCount++) });
            return false;
        });
    };

    function removeHoverDom(treeId, treeNode) {
        $("#addBtn_" + treeNode.tId).unbind().remove();
        $("#removeBtn_" + treeNode.tId).unbind().remove();
        $("#editBtn_" + treeNode.tId).unbind().remove();
    };

    var setting = {
        check: {
            enable: true
        },
        view: {
            addHoverDom: addHoverDom,
            removeHoverDom: removeHoverDom,
            dblClickExpand: false,
            showLine: true,
            selectedMulti: false
        },
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "pId",
                rootPId: ""
            }
        },
        callback: {
            beforeClick: function (treeId, treeNode) {
                var zTree = $.fn.zTree.getZTreeObj("tree");
                if (treeNode.isParent) {
                    zTree.expandNode(treeNode);
                    return false;
                } else {
                    demoIframe.attr("src", treeNode.file + ".html");
                    return true;
                }
            }
        }
    };

    //var zNodes = [
    //    { id: 3, pId: 0, name: "溯源管理", open: true },
    //    { id: 4, pId: 3, name: "静态溯源", file: "core/standardData" },
    //    { id: 23, pId: 3, name: "申请码", file: "core/simpleData" },
        

    //    { id: 5, pId: 0, name: "码管理", open: false },
    //    { id: 6, pId: 5, name: "码管理", file: "excheck/checkbox" },
    //    { id: 8, pId: 5, name: "绑码", file: "excheck/checkbox_nocheck" },
    //    { id: 13, pId: 5, name: "码下载", file: "excheck/checkbox_chkDisabled" },
        

    //    { id: 16, pId: 0, name: "系统管理", open: false },
    //    { id: 18, pId: 16, name: "机构管理", file: "exedit/drag" },
    //    { id: 19, pId: 16, name: "用户管理", file: "exedit/drag_super" },
    //    { id: 20, pId: 16, name: "角色管理", file: "exedit/drag_fun" },
    //    { id: 22, pId: 16, name: "菜单管理", file: "exedit/edit" },

    //     { id: 25, pId: 0, name: "数据统计", open: false },
    //    { id: 26, pId: 25, name: "防伪查询统计", file: "exedit/drag" },
    //    { id: 28, pId: 25, name: "溯源查询统计", file: "exedit/drag" }
         

         
    //];




    var zNodes =  
        [
       { id: 3, pId: 0, name: "溯源管理", open: true },
       { id: 4, pId: 3, name: "静态溯源", file: "core/standardData" },
       { id: 23, pId: 3, name: "申请码", file: "core/simpleData" },

       { id: 5, pId: 0, name: "码管理", open: false },
       { id: 6, pId: 5, name: "码管理", file: "excheck/checkbox" },
       { id: 8, pId: 5, name: "绑码", file: "excheck/checkbox_nocheck" },
       { id: 13, pId: 5, name: "码下载", file: "excheck/checkbox_chkDisabled" },

       { id: 16, pId: 0, name: "系统管理", open: false },
       { id: 18, pId: 16, name: "机构管理", file: "exedit/drag" },
       { id: 19, pId: 16, name: "用户管理", file: "exedit/drag_super" },
       { id: 20, pId: 16, name: "角色管理", file: "exedit/drag_fun" },
       { id: 22, pId: 16, name: "菜单管理", file: "exedit/edit" },

        { id: 25, pId: 0, name: "数据统计", open: false },
       { id: 26, pId: 25, name: "防伪查询统计", file: "exedit/drag" },
       { id: 28, pId: 25, name: "溯源查询统计", file: "exedit/drag" }


    ];





    $(document).ready(function () {
        var t = $("#tree");
        t = $.fn.zTree.init(t, setting, zNodes);
        
        //$.ajax({
        //    url: '/SysMenu/GetMenuForRole',
        //    type: 'post',
        //    dataType: 'json',
        //    data: {},
        //    success: function (data) {
        //        alert(JSON.stringify(data))
        //      //  $.fn.zTree.init($("#tree"), setting, JSON.stringify(data));
        //        //菜单未显示 是因为格式不是不是ztree规定的格式 
        //        //可以考虑使用新建实体类 改变其实体类属性值 
        //        t = $.fn.zTree.init(t, setting, JSON.stringify(data));
        //    }
        //})


        demoIframe = $("#testIframe");
        demoIframe.bind("load", loadReady);
        var zTree = $.fn.zTree.getZTreeObj("tree");
        zTree.selectNode(zTree.getNodeByParam("id", 101));

    });

    function loadReady() {
        var bodyH = demoIframe.contents().find("body").get(0).scrollHeight,
                htmlH = demoIframe.contents().find("html").get(0).scrollHeight,
                maxH = Math.max(bodyH, htmlH), minH = Math.min(bodyH, htmlH),
                h = demoIframe.height() >= maxH ? minH : maxH;
        if (h < 530) h = 530;
        demoIframe.height(h);
    }
    //新增菜单操作
    function Dosumbit() {

        var treeObj = $.fn.zTree.getZTreeObj("tree"),
           nodes = treeObj.getCheckedNodes(true),
           v = ""; idva = "";
        for (var i = 0; i < nodes.length; i++) {
            // v += nodes[i].name + ",";获取节点名称
            idva += nodes[i].id + ",";
        }
       
        $.ajax({
            url: '/SysMenu/SaveData',
            type:'post',
            data: { "roleId": $('#RoleInfo').val(), "menuId": idva },
            dataType:'json',
            async: false,
            success: function (data) {
                alert(data.Msg);
                window.location.href = '/SysRole/Index';
            }
        })
    }

</script>
<main class="main-cont content mCustomScrollbar">
    <div class="form-group-col-2">
        <div class="form-label">所属角色：</div>
        <div class="form-cont">
            @Html.DropDownListFor(m => m.ID, ViewData["selectRoleList"] as IEnumerable<SelectListItem>, new { @id = "RoleInfo", style = "width:30%;" })
             
            
        </div>
    </div>
    <button class="btn btn-secondary" onclick="Dosumbit()">基础按钮</button>

    <ul id="tree" class="ztree" style="width:560px;margin-left:300px; overflow:auto;"></ul>

</main>
